package com.hk.blog.config;

import com.hk.blog.domain.Permission;
import com.hk.blog.domain.Role;
import com.hk.blog.service.*;
import com.hk.commons.util.CollectionUtils;
import com.hk.commons.util.Constants;
import com.hk.commons.util.StringUtils;
import com.hk.core.authentication.api.menus.AbstractMenuManager;
import com.hk.core.authentication.api.menus.Menu;
import com.hk.core.authentication.api.menus.MenuManager;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author Kevin
 */
@Configuration
@EnableWebMvc
@RequiredArgsConstructor
public class BlogAutoConfiguration {

//    @Bean
//    public MenuManager menuManager() throws IOException {
//        return new DefaultConfigMenuManager(JsonUtils.deserializeList(
//                StreamUtils.copyToByteArray(new ClassPathResource("menu.json").getInputStream()),
//                Menu.class));
//    }

    private final MenuService menuService;

    private final PermissionMenuService permissionMenuService;

    private final PermissionService permissionService;

    private final RolePermissionService rolePermissionService;

    private final RoleService roleService;


    @Bean
    public MenuManager menuManager() {
        return new AbstractMenuManager() {
            @Override
            protected List<Menu> getAllMenu() {
                List<com.hk.blog.domain.Menu> menuList = menuService.findByParentId(Constants.DEFAULT_VALUE_LONG);
                List<Role> allRoles = roleService.findAll();
                List<Permission> allPerms = permissionService.findAll();
                return mapMenu(menuList, Collections.unmodifiableList(allRoles), Collections.unmodifiableList(allPerms));
            }

            private List<Menu> mapMenu(List<com.hk.blog.domain.Menu> menuList, List<Role> allRoles, List<Permission> allPerms) {
                return menuList.stream().map(item -> {
                    var menu = new Menu()
                            .setId(item.getId())
                            .setMenuName(item.getMenuName())
                            .setIcon(item.getIcon())
                            .setComponent(item.getComponent())
                            .setLink(item.getPath())
                            .setMeta(item.getMeta())
                            .setChildren(mapMenu(menuService.findByParentId(item.getId()), allRoles, allPerms));
                    if (StringUtils.isNotEmpty(item.getPath())) { //对于path为空的，属于折叠菜单，不需要查询权限
                        Set<Long> permissionIds = permissionMenuService.findPermissionIdsByMenuId(item.getId());
                        if (CollectionUtils.isNotEmpty(permissionIds)) {
                            List<Menu.Perms> perms = allPerms.stream()
                                    .filter(perm -> permissionIds.contains(perm.getId()))
                                    .map(permission -> new Menu.Perms().setValue(permission.getPermissionCode()))
                                    .collect(Collectors.toList());
                            menu.setPerms(perms);
                            Set<Long> roleIds = rolePermissionService.findRoleIdsByPermissionIds(permissionIds);
                            if (CollectionUtils.isNotEmpty(roleIds)) {
                                List<Menu.Role> roles = allRoles.stream()
                                        .filter(role -> roleIds.contains(role.getId()))
                                        .map(role -> new Menu.Role().setValue(role.getRoleCode()))
                                        .collect(Collectors.toList());
                                menu.setRoles(roles);
                            }
                        }
                    }
                    return menu;
                }).collect(Collectors.toList());
            }


        };
    }
}
